package com.google.code.expr.runtime;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

public class EXPRRunTimeState {
	private HashMap<String,EXPRVariable> mState = new HashMap<String, EXPRVariable>();
	
	public EXPRRunTimeState() {
		super();
	}

	public EXPRVariable getVariable(String pName) {
		return mState.get(pName);
	}
	
	public void setVariable(String pName, EXPRVariable pVar) {
		mState.put(pName, pVar);
	}
	public void setVariableFromString(String pName, String pValue) {
		EXPRVariable var = buildVariableFromString(pName, pValue);
		mState.put(pName, var);
	}
	private EXPRVariable buildVariableFromString(String pName, String pValue) {
		EXPRVariable var = new EXPRVariable(pName);
		EXPRValue value = null;
		if (pValue!=null) {
			value = new EXPRValue(EXPRValueConverter.parseUnknown(pValue));
		}
		var.setValue(value);
		return var;
	}
	
	public String toString() {
		StringWriter writer = new StringWriter();
		PrintWriter out = new PrintWriter(writer);
		for (String name : mState.keySet()) {
			out.println(mState.get(name));
		}
		return writer.toString();
	}
}
